package jrdcom.com.jrdmovie.Present;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import jrdcom.com.jrdmovie.JrdHttpMethod.Beans.JrdSubjectBean;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdHttpMethod;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdHttpResult;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdSubscriber.JrdOnActionListener;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdSubscriber.JrdOnNextListener;
import jrdcom.com.jrdmovie.JrdHttpMethod.JrdSubscriber.ProgressSubscriber;

/**
 * Created by longcheng on 2017/3/27.
 */

public class JrdTopPresent implements JrdTopMovieConstract.JrdTopMoviePresent{
    private List<JrdSubjectBean> jrdMovieArray = new ArrayList<>();
    private Context mContext;
    private JrdTopMovieConstract.JrdTopMoveView topMoveView;
    private int jrdIndex = 0;
    private int jrdCount = 10;
    private int jrdToatalCount = 0;
    private Boolean jrdIsLoading = false;
    public JrdTopPresent(Context context){
        mContext =context;
    }
    private JrdOnNextListener<JrdSubjectBean> onNextListener = new JrdOnNextListener<JrdSubjectBean>() {
        @Override
        public void OnNext(JrdSubjectBean jrdSubjectBean) {
            jrdMovieArray.add(jrdSubjectBean);

        }
    };

    private JrdOnNextListener<JrdHttpResult<List<JrdSubjectBean>>> onNextListenerList = new JrdOnNextListener<JrdHttpResult<List<JrdSubjectBean>>>() {
        @Override
        public void OnNext(JrdHttpResult<List<JrdSubjectBean>> jrdSubjectBeenlist) {
            jrdToatalCount = jrdSubjectBeenlist.getTotal();//jrdSubjectBeenlist.size();
            //jrdMovieArray = jrdSubjectBeenlist.getSubjects();
            jrdMovieArray.addAll(jrdSubjectBeenlist.getSubjects());
            jrdIndex += jrdCount;
            topMoveView.renderView(jrdMovieArray);
            jrdIsLoading = false;
        }
    };

    private JrdOnActionListener.JrdOnCompleteListener onCompleteListener = new JrdOnActionListener.JrdOnCompleteListener() {
        @Override
        public void OnComplete() {
            topMoveView.renderView(jrdMovieArray);
        }
    };
    public void getMovie(){
        List<JrdSubjectBean> list = null;

        ProgressSubscriber<JrdSubjectBean> progressSubscriber = new ProgressSubscriber<>(mContext, onNextListener,onCompleteListener);
        JrdHttpMethod.getInstance().getMovie(progressSubscriber, jrdIndex , jrdCount);
    }

    public void getMovieList(){
        //ProgressSubscriber<List<JrdSubjectBean>> progressSubscriber = new ProgressSubscriber<List<JrdSubjectBean>>(mContext, onNextListenerList);
        //JrdHttpMethod.getInstance().getMovieList(progressSubscriber, jrdIndex, jrdCount);
        ProgressSubscriber<JrdHttpResult<List<JrdSubjectBean>>> progressSubscriber = new ProgressSubscriber<JrdHttpResult<List<JrdSubjectBean>>>(mContext, onNextListenerList);
        //JrdHttpMethod.getInstance().getMovieList(progressSubscriber, jrdIndex, jrdCount);
        JrdHttpMethod.getInstance().getHttpMovieList(progressSubscriber, jrdIndex,jrdCount);

    }

    public void setView(JrdTopMovieConstract.JrdTopMoveView view){
        topMoveView = view;
    }

    public void getMovieExt(){
        if(jrdIndex < jrdToatalCount && false == jrdIsLoading){
            getMovieList();
            jrdIsLoading = true;
        }
    }


    public JrdSubjectBean getMovieByPosition(int position){
        return jrdMovieArray.get(position);
    }
}
